//
//  ZJCourseGradeView.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/7/17.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJCourseGradeView.h"
#import "LCStarRatingView.h"
@interface ZJCourseGradeView ()
/** label */
@property (nonatomic, strong) UILabel *label;
/** 分数 */
@property (nonatomic, strong) UILabel *grade;
/** 人数 */
@property (nonatomic, strong) UILabel *number;
/** 评分滑动条 */
@property (nonatomic, strong) LCStarRatingView *starRatingView;
/** button */
@property (nonatomic, strong) UIButton *button;
/** 下划线 */
@property (nonatomic, strong) UILabel *underLine;
@end

@implementation ZJCourseGradeView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.label];
        [self addSubview:self.grade];
        [self addSubview:self.underLine];
        [self addSubview:self.number];
        [self addSubview:self.starRatingView];
        [self addSubview:self.button];
        
        self.label.sd_layout
        .leftSpaceToView(self, 12 * [UIScreen zj_screenHScale])
        .topSpaceToView(self, 0)
        .bottomSpaceToView(self, 0)
        .widthIs([NSString zj_sizeOfString:self.label.text font:self.label.font width:[UIScreen zj_screenWith]].width);
        
        self.grade.sd_layout
        .topEqualToView(self.label)
        .bottomEqualToView(self.label)
        .leftSpaceToView(self.label, 5 * [UIScreen zj_screenWScale])
        .widthIs([NSString zj_sizeOfString:self.grade.text font:self.grade.font width:[UIScreen zj_screenWith]].width);
        
        self.number.sd_layout
        .topSpaceToView(self, 3 * [UIScreen zj_screenHScale])
        .leftSpaceToView(self.grade, 5 * [UIScreen zj_screenWScale])
        .widthIs(100 * [UIScreen zj_screenWScale])
        .heightIs([NSString heightWithTextFont:self.number.font]);
        
        self.starRatingView.sd_layout
        .topSpaceToView(self.number, 3 * [UIScreen zj_screenHScale])
        .leftEqualToView(self.number)
        .bottomSpaceToView(self, 7 * [UIScreen zj_screenHScale])
        .widthIs(150 * [UIScreen zj_screenWScale]);
        
        
        self.button.sd_layout
        .topSpaceToView(self, 5 * [UIScreen zj_screenHScale])
        .rightSpaceToView(self, 12 * [UIScreen zj_screenWScale])
        .bottomSpaceToView(self, 5 * [UIScreen zj_screenHScale])
        .widthIs(50 * [UIScreen zj_screenWScale]);
        
        self.underLine.sd_layout
        .leftSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .bottomSpaceToView(self, 0)
        .heightIs(1);
    }
    return self;
}


#pragma mark - 点击事件
- (void)action_button:(UIButton *)button{
    NSString *score = [NSString stringWithFormat:@"%.1lf",self.starRatingView.progress];
    if (self.delegate) {
        [self.delegate ZJCourseGradeViewCommentNumber:score];
    }
    
}



#pragma mark - Setter
- (void)setModel:(ZJSCSynopsisGradeModel *)model{
    _model = model;
    if ([model.ISEVALUATE integerValue] == 1) {
        NSString *number = [model.APPRAISEDETAIL[0] objectForKey:@"SCORE"];
        self.starRatingView.progress = [number floatValue];
        self.starRatingView.userInteractionEnabled = NO;
        [self.button setTitle:@"已评" forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.button.backgroundColor = [UIColor viewBGGrayColor];
        self.button.userInteractionEnabled = NO;
    }else{
        self.starRatingView.progress = 0;
        self.starRatingView.userInteractionEnabled = YES;
        [self.button setTitle:@"评分" forState:UIControlStateNormal];
        [self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        self.button.backgroundColor = [UIColor viewBGGrayColor];
        self.button.userInteractionEnabled = YES;
    
    }
    
    self.grade.text = model.GRADE;
    self.grade.sd_layout.widthIs([NSString zj_sizeOfString:self.grade.text font:self.grade.font width:[UIScreen zj_screenWith]].width);
    [self.grade updateLayout];
    self.number.text = [NSString stringWithFormat:@"%@人评论",[NSString string:model.P_TOTAL isNilStringReturnDeftString:@"0"]];
    
    
}

#pragma mark - Getters
- (UILabel *)label{
    if (_label == nil){
        _label = [UILabel new];
        _label.textColor = [UIColor textBlackColor];
        _label.font = [UIFont titleTextFont];
        _label.text = @"课程评分 :";
        
    }
    return _label;
}

- (UILabel *)grade{
    if (_grade == nil){
        _grade = [UILabel new];
        _grade.textColor = [UIColor textBlackColor];
        _grade.font = [UIFont systemFontOfSize:20 * [UIScreen  zj_textFontScale]];
        
    }
    return _grade;
}

- (UILabel *)number{
    if (_number == nil){
        _number = [UILabel new];
        _number.textColor = [UIColor textBlackColor];
        _number.font = [UIFont systemFontOfSize:13 * [UIScreen zj_textFontScale]];
        
    }
    return _number;
}

- (LCStarRatingView *)starRatingView{
    if (_starRatingView == nil){
        _starRatingView = [LCStarRatingView new];
        _starRatingView.type = LCStarRatingViewCountingTypeFloat;
        _starRatingView.starMargin = 10.0;
        _starRatingView.starColor = [UIColor zj_colorWithHexString:@"ffc424"];
        _starRatingView.starPlaceHolderBorderWidth = 1;
        _starRatingView.starPlaceHolderColor = [UIColor whiteColor];
        _starRatingView.starPlaceHolderBorderColor = [UIColor zj_colorWithHexString:@"ffc424"];
       
    }
    return _starRatingView;
}

- (UIButton *)button{
    if (_button == nil){
        _button = [UIButton new];
        _button.titleLabel.font = [UIFont titleTextFont];
        _button.layer.cornerRadius = 4 * [UIScreen zj_screenWScale];
        [_button addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}
- (UILabel *)underLine{
    if (_underLine == nil){
        _underLine = [UILabel new];
        _underLine.backgroundColor = [UIColor viewBGGrayColor];
        
    }
    return _underLine;
}
@end
